package com.bjpowernode.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bjpowernode.DlykServerApplication;
import com.bjpowernode.constant.DicEnum;
import com.bjpowernode.entity.TDicValue;

import java.util.List;

public class SourceConverter implements Converter<Integer> {
    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        //获取excel中的值
        String excelValue = cellData.getStringValue();

        //从数据字典中查询称呼的值
        List<TDicValue> tDicValueList = (List<TDicValue>) DlykServerApplication.cacheMap.get(DicEnum.SOURCE.getCode());

        //从数据字典缓存中查询对应的称呼id
        for (TDicValue tDicValue : tDicValueList) {
            if (excelValue.equals(tDicValue.getTypeValue())) {
                return tDicValue.getId();
            }
        }

        //若程序执行到这里，则说明用户在excel中填写了数据字典不存在的数据
        return tDicValueList.get(0).getId();
    }
}
